//4070之后添加规格
var me;
var tagArr=[];
var homeIndex = {
	detail : {
		init : function(params) {
			 
			me = this;
			var _global = params;
			// 隐藏选择餐具层
			$('#tablewareDiv').css("display","none");
			// 菜单
			var menuData;
			var shoppingCarKey = 'shoppingCar_' + _global.BUSINESS_ID;
			
			var lastAddTime=localStorage.getItem("lastAddTime");
			if(typeof(lastAddTime)=="undefined"||lastAddTime==null){//没有添加过
				localStorage.setItem("lastAddTime",new Date().getTime());
			}else{
				var nowTime=new Date().getTime();
			 
				if(nowTime-lastAddTime>7200000){//2个小时
					homeIndex.localData(shoppingCarKey, {
						key : _global.RAMADHIN,
						remove : true
					});
					sessionStorage.clear();
					localStorage.setItem("lastAddTime",new Date().getTime());
				}
			}
			 
			var localCar = homeIndex.localData(shoppingCarKey);
			if (localCar.length) {
				if (!homeIndex.localData(shoppingCarKey)[_global.RAMADHIN]) {
					// 没有商家信息添加
					homeIndex.localData(shoppingCarKey, {
						key : _global.RAMADHIN,
						value : []
					});
				}
			} else {
				if (!homeIndex.localData(shoppingCarKey)[_global.RAMADHIN]) {
					// 没有商家信息添加
					homeIndex.localData(shoppingCarKey, {
						key : _global.RAMADHIN,
						value : []
					});
				}
			}
			 
			// 用户的购物车
			var shoppingCart = homeIndex.localData(shoppingCarKey)[_global.RAMADHIN];
			// 全局异步
			$.ajaxSettings.async = false;
			// 初始化数据
			$.get(rootpath + '/jhh/getShopMenus.do', {
				businessId : _global.BUSINESS_ID
			}, function(result) {
				me.menuData = result.shopMenus;
			}, 'json');
			// 停业
			if (_global.DISABLED == '0') {
				$('#outLayer').show();
				$(".mui-popup-button").on('click', function() {
					$('#outLayer').hide();
				});
				$('.order-closemask').css('display', 'block');
				$('.bottom-confirm').attr('disabled', "true");
			}
			//桌牌被禁用
			if (_global.DINING_STATUS == '0') {
				$('#outDiningLayer').show();
				$(".mui-popup-button").on('click', function() {
					$('#outDiningLayer').hide();
				});

				$('.order-closemask').css('display', 'block');
				$('.bottom-confirm').attr('disabled', "true");
			}
			// 本店暂停营业
			if (_global.OPERATING_STATUS ==1) {
				$('#layer-close').show();
				$(".mui-popup-button").on('click', function() {
					$('#layer-close').hide();
				});
				$('.order-closemask').css('display', 'block');
				$('.bottom-confirm').attr('disabled', "true");
			}

			// 刷新购物车
			me.refreshLocalShoppingCar = function() {
				homeIndex.localData(shoppingCarKey, {
					key : _global.RAMADHIN,
					value : shoppingCart
				});
			}
		 
			// 添加到购物车
			me.addShoppingCart = function(food) {
				var menuId="";
				var quantity = food.food.quantity;
				var exist=0;
				for (i = 0; i < shoppingCart.length; i++) {
					if (shoppingCart[i].food.foodId == food.food.foodId) {
						menuId=shoppingCart[i].food.menuId;
						var carFoodCount = shoppingCart[i].food.quantity + quantity ;
						if(carFoodCount<=0){
							shoppingCart.splice(i, 1);//移除
						}else{
							food.food.quantity=carFoodCount;
							//更新购物车
							shoppingCart[i].food=food.food; 
						}
						exist=1;
						break;
					} 
				}
				 
				if(exist==0){
					menuId=food.menuId;
					shoppingCart.push({
						food:food.food,
						menuName : food.menuName,
						menuSort:food.menuSort,
						menuId : food.menuId
					});
				}
				//更改菜单角标 购物车数量 价格
				me.setCarNum(quantity);
				if(quantity<0){
					me.setCarTotalPrice(0-food.food.price);
				}else{
					me.setCarTotalPrice(food.food.price);
				}
				var menucategory = $("a[parent='parent" + menuId + "']");
				me.setMenucategory(parentId, menucategory, quantity);
				me.refreshLocalShoppingCar();
			};
			$.ajaxSetup({
				async : true
			});
			mui.init({
				// 启用右滑关闭功能
				swipeBack : true
			});
			
			var segmentedControls = $('#segmentedControls');
			var segmentedControlContents = $('#segmentedControlContents');
			var html = [];
			 
			// 左侧菜单
			for (i = 0; i < me.menuData.length; i++) {
				var data = me.menuData[i];
				//判断特价
//				if(data.SPECAIL_ABLE==1){
//					//特价
//					html.push('<a class="mui-control-item" parent="parent' + data.id + '" data-index="' + i + '" href="#content' + data.id + '"><span class="control-item-listtext listtextright"><i class="tjc"><img src="'+rootpath+'/static/myjs/information/weixin/images/tjc@2x.png"></i>&nbsp;&nbsp;' + data.NAME + '</span></a>');
//				}else{
					html.push('<a class="mui-control-item" parent="parent' + data.foodMenuId + '" data-index="' + i + '" href="#content' + data.foodMenuId + '"><span class="control-item-listtext">' + data.name + '</span></a>');
//				}
			}
		 
			// 添加左侧菜单
			segmentedControls.append(html);
			html = '';
			for (i = 0; i < me.menuData.length; i++) {
				var data = me.menuData[i];
				if (data.children.length > 0) {
					html += '<div id="content' + data.foodMenuId + '" class="mui-control-content2">';
					html += '<p class="titlename" style="color:#ff8428;font-size:12px">' + data.name +'<span style="color:#666;font-size:10px">&nbsp;&nbsp;&nbsp;&nbsp;'+data.menuDesc
+'</span>'+'</p>';
					html += '<ul class="mui-table-view">';
					for (j = 0; j < data.children.length; j++) {
						var children = data.children[j];
						//特卖的 商品id=商品id_商家id/特卖的不存进购物车，只做展示用，
						var tempFood = null;
						for (k = 0; k < shoppingCart.length; k++) {
							var cartFood = shoppingCart[k];
						//	foodId.split("_")[0]
							if (cartFood.food.foodId == children.foodId) {
								tempFood = cartFood.food;
							}
						}
						//菜品没有图的默认图
						var imagePath= children.img ;
						if(typeof(imagePath)=="undefined"||imagePath==""||imagePath==null){
							imagePath="shop/imgtou.png";
						} 
						imagePath=imgPath+imagePath;
						//imagePath="https://p9.pstatp.com/weili/s/264483459099262993.webp";
						// 售罄的
						if (children.disabled == 1) {
							html += '<li class="mui-table-view-cell gray-text" style="height:100px">';
							html += '<div class="order-mask"></div>';
							html += '<div class="mui-pull-left order-menuimg"><img class="mui-media-object mui-pull-left gray-img" src="' +imagePath + '" style="max-width:76px"/></div>';
							html += '<div class="mui-media-body">';
							html += '<p class="mui-two-ellipsis gray-text">' + children.name+ '</p>';
							html += '<span style="font-size:10px;color:#999;display:block;height:15px;line-height:15px;margin-top:3px;"">' + children.subTitle + '</span>';
							html += '<div class="price-box1" price="' + children.price + '" foodName="' + children.name + '" foodId="' + children.foodId + '" parentId="' + data.foodMenuId + '" description="' + children.foodDesc + '" style="position:absolute;top:60px;">';
							html += '<div class="price"><span class="price-text"><i>￥</i>' + children.price + '</span></div>';
							html += '</div>';
							html += '</div>';
							html += '</li>';
						} else {
							// 未售罄的
							
//							if(children.FOOD_TYPE==5){//特价的
//								var originalPrice=children.PRICE;
//								children.PRICE=new Decimal(children.ORIGINAL_PRICE).times(0.01) ;
//								children.ORIGINAL_PRICE=originalPrice;
//							}
							//var discountSpecial=new Decimal(children.price).div(children.price).times(10).toDP(1, 0);
							
							html += '<li class="mui-table-view-cell">';
							html += '<div class="mui-pull-left order-menuimg"><img class="Details mui-media-object mui-pull-left" src="' + imagePath+ '" style="max-width:76px"/></div>';
							html += '<div class="mui-media-body">';
							html += '<p class="mui-two-ellipsis">' + children.name+ '</p>';
							html += '<span style="font-size:10px;color:#999;display:block;height:15px;line-height:15px;margin-top:3px;">' + children.subTitle + '</span>';
//							if(children.FOOD_TYPE==5){
//								html += '<p class="limit"><span class="bargin-num">'+discountSpecial+'折</span><span class="bargin-txt">限时抢购</span></p>';
//							}else{
								html += '<p style="height:17px;"></p>';
//							}
							//普通的id=
							var ftdId='ftd'+children.foodId;
//								if(data.foodMenuId==_global.BUSINESS_ID ){
//									//特卖的id=
//									ftdId='tm'+children.foodId;
//								}
								if( children.foodDesc==''){
									 children.foodDesc="暂无";
								}
							html += '<div id="'+ftdId+'"  class="price-box"  isMultiType="'+children.isMulttag+'" price="' + children.price+ '" menuSort="' + data.sorts+ '" foodSort="' + children.sorts + '" foodName="' + children.name + '" foodId="' + children.foodId + '" parentId="' + data.foodMenuId + '" parentName="' + data.name + '" description="' + children.foodDesc + '">';
							//多规格
							if(children.isMulttag==1){
								var param='href=javascript:mutliType("'+children.foodId+'","'+children.name+'",'+children.price+')' ; 
								html += '<div class="price"><span class="color-orange price-text"><i>￥</i>' + children.price + '<b>&nbsp;起</b></span>';
//								if(children.FOOD_TYPE==5){
//									html+='<span class="delate-price">￥'+children.ORIGINAL_PRICE+'</span>';
//								}
								html +='</div>';
								if (tempFood) {
									html += '<div class="choose-box" ><div  class="choose-boxbadge"><a class="standard"  '+param+'>选规格</a><span class="mui-badge mui-badge-orange">' + tempFood.quantity + '</span></div></div>';
								} else {
									html += '<div class="choose-box" ><div  class="choose-boxbadge" id="tagQ'+children.foodId+'"><a class="standard"  '+param+'>选规格</a></div></div>';
								}
							}else{
								html += '<div class="price"><span class="color-orange price-text"><i>￥</i>' + children.price + '</span>';
//								if(children.FOOD_TYPE==5){
//									html+='<span class="delate-price">￥'+children.ORIGINAL_PRICE+'</span>';
//								}
								html+='</div>';
								if (tempFood) {
									html += '<div class="choose-box"><span class="iconfont icon-tubiao"></span><em  onclick= checkNum(\''+children.foodId+'\',\''+children.name+'\',\''+children.price+'\',this) id="num_'+children.foodId+'" class="choose-number number_'+children.foodId+'">' + tempFood.quantity + '</em><span class="iconfont icon-tianjia tianjia add1"></span></div>';
								} else {
									html += '<div class="choose-box"><span class="iconfont icon-add tianjia add1"></span></div>';
								}
							}
							html += '</div>';
							html += '</div>';
							html += '</li>';
						}
					}
					html += '</ul></div>';
				}else{
					html += '<div id="content' + data.foodMenuId + '" class="mui-control-content2">';
					html += '<p class="titlename" style="color:#FF7260;font-size:12px">' + data.name +'<span style="color:#666;font-size:10px">&nbsp;&nbsp;&nbsp;&nbsp;'+data.menuDesc
+'</span>'+'</p>';
					html += '<ul class="mui-table-view">';
					html += '</ul></div>';
				}
			}
			segmentedControlContents.append(html);
			// 选中第一个菜单
			segmentedControls.children("a:first-child").addClass('mui-active');
			segmentedControlContents.children("div:first-child").addClass('mui-active');
			
			
			// 购物车上面的小图标
			var carNum = $(".shopping-carnum");
			me.setCarNum = function(num) {
				if (num > 0) {
					carNum.html(parseInt(carNum.html()) + 1);
				} else {
					if (parseInt(carNum.html()) == 0) {
						return;
					} else {
						carNum.html(parseInt(carNum.html()) - 1);
					}
				}
			};
			
			// 购物车总价
			var carTotalPrice = $('#carTotalPrice');
			me.setCarTotalPrice = function(price) {
				var currentTotalPrice = parseFloat(carTotalPrice.text() == '' ? 0 : carTotalPrice.text());
				if (currentTotalPrice >= 0) {
					var totalPrice = parseFloat((currentTotalPrice + parseFloat(price)).toFixed(2));
					if(totalPrice>=0){
						carTotalPrice.text(totalPrice);
					}
				}
			};

			// 菜单上面的数量显示  foodMenuId   obj   1/-1
			me.setMenucategory = function(id, obj, number) {
				var menucategoryId = '#menucategory' + id;
				if ($(menucategoryId).length > 0) {
					$(menucategoryId).text(parseInt($(menucategoryId).text()) + parseInt(number));
					if (parseInt($(menucategoryId).text()) == 0) {
						$(menucategoryId).remove();
					}
				} else {
					if (number > 0) {
						obj.append('<span class="mui-badge mui-badge-orange" id="menucategory' + id + '">' + number + '</span>');
						$(".bottom-confirm").show();
						$("#end").show();
						$("#Total").show();
						$("#Totalempty").hide();
					}
				}
			};

			// 刷新菜单上面显示的数字
			me.refreshMenucategory = function() {
				var menuContent2 = $(".mui-control-content2");
				for (var i = 0; i < menuContent2.length; i++) {
					var parentId = $(menuContent2[i]).attr('id').substr(7);
					var menucategory = $("a[parent='parent" + parentId + "']");
					var chooseNumerArr = $(menuContent2[i]).find('.choose-number');
					for (var q = 0; q < chooseNumerArr.length; q++) {
						me.setMenucategory(parentId, menucategory, $(chooseNumerArr[q]).text());
					}
				}
			}
			
			//遍历购物车
			if (shoppingCart.length > 0) {
				me.refreshMenucategory();
				var tempCarNum = 0;
				var tempTotalPrice = 0;
				var disableFoods=[];
				var disableFoodTags="";
				for (var i = 0; i < shoppingCart.length; i++) {
					var foodCount=shoppingCart[i].food.quantity ;
					if (foodCount > 0) {
						var foodIds=shoppingCart[i].food.foodId.split("_");
						
						if(typeof($("#ftd"+foodIds[0]).html())=="undefined"){//售罄的   禁用的不显示在列表里
							disableFoods[i]=i;
							continue;
						}else{
							if(foodIds.length>1){//多规格
								if($("#ftd"+foodIds[0]).attr("isMultiType")=='1'){//还是多规格
									var foodQUANTITY=foodCount;
									if (typeof(tagArr[foodIds[0]]) == "undefined") {
										tagArr[foodIds[0]]= foodQUANTITY;
									}else{
										tagArr[foodIds[0]]=tagArr[foodIds[0]]+foodQUANTITY;
									} 
									sessionStorage.setItem("food"+shoppingCart[i].FOOD_ID,foodQUANTITY);
									//更新购物车中多规格名称和价格
									disableFoodTags+=","+foodIds[1];
								}else{
									disableFoods[i]=i;
									continue;
								}
							}else{//普通商品
								if($("#ftd"+foodIds[0]).attr("isMultiType")=='1'){//变成了多规格
									disableFoods[i]=i;
								}else{
									shoppingCart[i].PRICE=$("#ftd"+foodIds[0]).attr("price");//替换价格
									shoppingCart[i].NAME=$("#ftd"+foodIds[0]).attr("foodName");//替换价格
								}
							}
						}
						
						tempCarNum += foodCount;
						var foodPrice = new Decimal(parseFloat(shoppingCart[i].food.price)).times(foodCount);  
						tempTotalPrice = new Decimal(parseFloat(tempTotalPrice)).plus( parseFloat(foodPrice));
					}

				}
				/*
				if(disableFoodTags!=""){
					disableFoodTags=disableFoodTags.substring(1);
					var disableFoodArr=[];
					$.post(rootpath + '/m/home/listByIds', {
						FOOD_TAG_IDS: disableFoodTags
					}, function(result) { 
						var tagArrLine= result.dataset_line;
						var totalAmount=new Decimal(0);
						var totalCount=0;
						var disableCount=0;
						var tagArrCount=[];
						
						//遍历购物车 替换购物车中规格的名称和价格
						for (var i = 0; i < shoppingCart.length; i++) {
							var foodIds=shoppingCart[i].food.foodId.split("_");
							if(foodIds.length>1){
								//多规格
								for (var j= 0; j < tagArrLine.length;j++) {
									if(tagArrLine[j].foodTagsId==foodIds[1]){
										 if(tagArrLine[j].ISDELETE=="0"){
											 disableFoodArr[i]=i;
											 var parentId= $("#ftd"+foodIds[0]).attr("parentId");
												var menucategory = $("a[parent='parent" + parentId+ "']");
												me.setMenucategory(parentId, menucategory, -shoppingCart[i].QUANTITY);
												//多规格右上角数字回显，菜单数字回显
												//改变规格右上角数量 
												changeTagQ(shoppingCart[i].FOOD_ID,-shoppingCart[i].QUANTITY);
											 sessionStorage.setItem("food"+shoppingCart[i].FOOD_ID,0);
										 }else{
											 	if (typeof(tagArrCount[foodIds[0]]) == "undefined") {
											 		tagArrCount[foodIds[0]]= shoppingCart[i].QUANTITY;
												}else{
													tagArrCount[foodIds[0]]=tagArrCount[foodIds[0]]+shoppingCart[i].QUANTITY;
												} 
											 shoppingCart[i].PRICE=tagArrLine[j].PRICE;//替换价格
											 shoppingCart[i].NAME=shoppingCart[i].NAME.split('_')[0]+"_"+tagArrLine[j].TAGS_NAME;//替换名称
											 
											 totalCount+=shoppingCart[i].QUANTITY;
											 totalAmount=totalAmount.plus(new Decimal(shoppingCart[i].PRICE).times(shoppingCart[i].QUANTITY) );
										 }
									}
								}
							}else{
								 totalCount+=shoppingCart[i].QUANTITY;
								totalAmount=totalAmount.plus(new Decimal(shoppingCart[i].PRICE).times(shoppingCart[i].QUANTITY) );
							} 
						}
						
						if(disableFoodArr.length>0){
							for (var x in disableFoodArr){
								shoppingCart.splice(x,1);
							}
						}
						
						me.refreshLocalShoppingCar();
						
						// 购物车上的数字
						var carNum = $(".shopping-carnum");
						carNum.html(totalCount);
						// 购物车总价
						var carTotalPrice = $('#carTotalPrice');
						carTotalPrice.text(parseFloat(totalAmount));
						
					}, 'json');
				}*/
				
				
				if(disableFoods.length>0){
					for (var x in disableFoods){
						shoppingCart.splice(x,1);
					}
					me.refreshLocalShoppingCar();
				}
				
				
				// 购物车上的数字
				var carNum = $(".shopping-carnum");
				carNum.html(tempCarNum);

				// 购物车总价
				var carTotalPrice = $('#carTotalPrice');
				carTotalPrice.text(parseFloat(tempTotalPrice));
				
				//多规格右上角数字回显，菜单数字回显
				//改变规格右上角数量 
				for(var key in tagArr){
					var parentId= $("#ftd"+key).attr("parentId");
					var menucategory = $("a[parent='parent" + parentId+ "']");
					me.setMenucategory(parentId, menucategory, tagArr[key]);
					$("#tagQ"+key).append('<span class="mui-badge mui-badge-orange">'+tagArr[key]+'</span>');
			    }
			}

			// 商品详情
			$(".Details").on('click', function() {
				var priceBox = $(this).parent().parent().find(".price-box");
				$('#layer-information').show();
				//商品名
				$('.information-content').find('#informationFood').text(priceBox.attr('foodname'));
				$('.information-content').find('#informationPrice').text('￥' + priceBox.attr('price'));
				$('.information-content').find('#description').text(priceBox.attr('description'));
				var showsrc=$(this).attr('src');
				$('.information-content').find('.picturebox img').attr('src', showsrc.replace("@35p","@80p"));
				$('.information-content').show();
			})
			$(".layer-closebtn").on('click', function() {
				$('#layer-information').hide();
				$('.information-content').hide();
			})
			$("#layer-information").on('click', function() {
				$('#layer-information').hide();
				$('.information-content').hide();
				$('#tablewareDiv').hide();
				$('.prompt-add').unbind("click");
				$('.prompt-subtract').unbind("click");
				$('#tablewareDivBtn').unbind("click");
			})
			//关闭弹框
			$('.tableware-close').on('click', function() {
				 $("#layerType").hide();
			});
			$('#layerType').on('click', function(e) {
				if(e.target==this){
					$("#layerType").hide();
				}
			});
			// 绑定减少事件
			me.bindReduceEvent = function() {
				$("span[class='iconfont icon-tubiao']").unbind("click");
				// 绑定减少
				$("span[class='iconfont icon-tubiao']").on('click', function() {
					var parent = $($(this).parent().parent());
					var parentId= parent.attr('parentId');
					var foodId=parent.attr('foodId');
					//特卖
//					if(parentId==_global.BUSINESS_ID){
//						reducetm(parent);
//						reduceftd($("#ftd"+foodId));
//					}else{
						reduceftd(parent);
//						reducetm($("#tm"+foodId));
//					}
				});
			}
			me.bindReduceEvent();
			//减少特卖
			function reducetm(parent){
				var thisobj=parent.find("span[class='iconfont icon-tubiao']");
				var amount = thisobj.next();
				var num = parseFloat(amount.html());
				if (num >=1) {
					amount.html(num - 1);
					var foodId=parent.attr('foodId');
					var parentId= parent.attr('parentId');
					if(typeof(foodId)=="undefined"){
						return;
					}
					var menucategory = $("a[parent='parent" + parentId + "']");
					me.setMenucategory(parentId, menucategory, -1);
					
					if (parseFloat(amount.html()) == 0) {
						amount.next().attr('class','iconfont icon-add tianjia add1');
						amount.remove();
						thisobj.remove();
						changeTagQ(foodId,-1);
					}else{
						changeTagQ(foodId,-1);
					}
				}
			}
			//减少普通菜
			function reduceftd(parent){
				var thisobj=parent.find("span[class='iconfont icon-tubiao']");
				var amount = thisobj.next();
				var num = parseFloat(amount.html());
				if (num >=1) {
					amount.html(num - 1);
					var foodId=parent.attr('foodId');
					var parentId= parent.attr('parentId');
					var price=parent.attr('price');
					var foodName=parent.attr('foodName');
					var flag=0;
					var obj;
					if (typeof(parent.attr('foodId')) == "undefined") {
						foodId=$("#tagId").val();
						obj=$("#ftd"+foodId.split("_")[0]);
						parentId=obj.attr("parentId");
						foodName=$("#tagName").val(); 
						price=$("#tagPrice").val();
						flag=1;
					}  
					 
					me.addShoppingCart({
						food:{foodId : foodId,
							quantity : -1,
							name : foodName,
							price : price
							}
					});
					if (parseFloat(amount.html()) == 0) {
						amount.next().attr('class', 'iconfont icon-add tianjia add1');
						amount.remove();
						thisobj.remove();
						if(flag==1){
							sessionStorage.setItem("food"+foodId,"0");
							$("#cartBox").show();
							$("#chooseBox").hide();
						}
						changeTagQ(foodId,-1);
					}else{
						changeTagQ(foodId,-1);
						sessionStorage.setItem("food"+foodId,num-1+"");
					}
					me.refreshLocalShoppingCar();
				}

				if (shoppingCart.length == 0) {
					$(".bottom-confirm").hide();
					$("#end").hide();
					$("#Total").hide();
					$("#Totalempty").show();
				}
			}
			
			// 绑定添加
			$(".iconfont.tianjia").on('click', function() {
				 
				localStorage.setItem("lastAddTime",new Date().getTime());
				var parent = $($(this).parent().parent());
				var thisobj=parent.find(".tianjia");
				
				if (thisobj.hasClass('prompt-add')) {
					return;
				}
			 
				var parentId= parent.attr('parentId');
				var foodId=parent.attr('foodId');
				 
				//特卖
//				if(parentId==_global.BUSINESS_ID){
//					addtm(parent);
					addftd($("#ftd"+foodId));
//				}else{
//					addftd(parent);
//					addtm($("#tm"+foodId));
//				}
			});
			//添加普通商品
			function addftd(parent){
				var thisobj=parent.find(".tianjia");
				if (thisobj.hasClass('prompt-add')) {
					return;
				}
				//商品数量
				var amount = thisobj.prev();
				var parent = $(thisobj.parent().parent());
				var foodId=parent.attr('foodId');
				var parentId= parent.attr('parentId');
				var parentName= parent.attr('parentName');
				var price=parent.attr('price');
				var foodName=parent.attr('foodName');
				var foodSort=parent.attr('foodSort');
				var MenuSort=parent.attr('menuSort');
			
				var obj=thisobj;
				var foodCount="1";
				//多规格
				if (typeof(parent.attr('foodId')) == "undefined") {
					foodId=$("#tagId").val();
					if(foodId==""){
						return;
					}
					obj=$("#ftd"+foodId.split("_")[0]);
//					if(typeof(obj=="undefined")){
//						//特卖商品id
//						obj=$("#tm"+key);
//					}
					parentId=obj.attr("parentId");
					
					parentName=obj.attr("parentName");
					foodName=$("#tagName").val();
					price=$("#tagPrice").val();
					foodSort=obj.attr("foodSort");
					MenuSort=obj.attr("menuSort");
					thisobj=$("#amountBox");
					amount= thisobj.prev();
				}   
				 
				if (amount.length == 0) {
					thisobj.before('<span class="iconfont icon-tubiao"></span><em onClick=checkNum(\''+foodId+'\',\''+foodName+'\',\''+price+'\',this) id="num_'+foodId+'" class="choose-number number_' +foodId + '">1</em>');
					thisobj.removeClass("icon-add");
					thisobj.addClass("icon-tianjia");
					changeTagQ(foodId,1);
				}else{
					var amountFloat=parseFloat(amount.html()) + 1;
					amount.html(amountFloat);
					changeTagQ(foodId,1);
					foodCount=amountFloat+"";
				}
			
				//更改菜单的数量和价格
				sessionStorage.setItem("food"+foodId,foodCount);
				//更改购物车的数量和价格
				// 添加到购物车
				me.addShoppingCart({
					food:{
						foodId : foodId,
						quantity : 1,
						name : foodName,
						price : price,
						foodSort :foodSort 
					},
					menuSort :MenuSort,
					menuName : parentName,
					menuId : parentId
				});
				 
				me.bindReduceEvent();
			}
			//添加特卖商品
			function addtm(parent){
				var thisobj=parent.find(".tianjia");
				if (thisobj.hasClass('prompt-add')) {//购物车
					return;
				}
				//商品数量
				var amount =parent.find(".tianjia").prev();
				var foodId=parent.attr('foodId');
				var foodName=parent.attr('foodName');
				var price=parent.attr('price');
				var parentId= parent.attr('parentId');
				if(typeof(foodId)=="undefined"){
					return;
				}
				var foodCount="1";
				if (amount.length == 0) {
					thisobj.before('<span class="iconfont icon-tubiao"></span><em onClick=checkNum(\''+foodId+'\',\''+foodName+'\',\''+price+'\',this) id="num_'+foodId+'" class="choose-number number_' +foodId + '">1</em>');
					thisobj.removeClass("icon-add");
					thisobj.addClass("icon-tianjia");
					changeTagQ(foodId,1); 
				}else{
					var amountFloat=parseFloat(amount.html()) + 1;
					amount.html(amountFloat);
					changeTagQ(foodId,1);
					foodCount=amountFloat+""; 
				}
				var menucategory = $("a[parent='parent" + parentId + "']");
				me.setMenucategory(parentId, menucategory, 1);
				 
				me.bindReduceEvent();
			}
			
			
			
			// 选好了
			$('.bottom-confirm').on('click', function() {
				if (shoppingCart.length == 0) {
					mui.toast('购物车快饿瘪了 T.T', {
						duration : 'long',
						type : 'div'
					})
					return;
				}
				var submitShoppingCar = function() {
					// 提交购物车内容到服务器
					$('.bottom-confirm').attr('disabled', "true"); // 禁用
					$('.bottom-confirm').html('提交中');
 
					location.href = rootpath+'/jhh/toShopping?dining='+_global.trueRamadhin;
				}
				// 菜品提示
				if (_global.promptFoods.length) {
					var promptFoods = _global.promptFoods;
					for (var i = 0; i < promptFoods.length; i++){
						var food = promptFoods[i];
						var promptFood = $("div[foodid='" + food.FOOD_ID + "']");
						if (promptFood.find('.choose-number').length == 0){
							promptFoodsShow(promptFoods);
							return;
						}
					}
					submitShoppingCar();
				} else {
					submitShoppingCar();
				}
				
				//显示消费必选信息
				function promptFoodsShow(promptFoods){
					var html = "";
					$('#layer-information').show();
				    $('#tablewareDiv').show();
					if($('#tablewareUl').find("li").length == 0){
						for (var int = 0; int < promptFoods.length; int++) {
							var food = promptFoods[int]; 
							var promptFood = $("div[foodid='" + food.FOOD_ID + "']");
							var number =promptFood.find('.choose-number').html();
							
							var filePath= food.FILEPATH ;
							if(typeof(filePath)=="undefined"||filePath==""){
								filePath="default/img_tou2.png";
							}
							if(typeof(number)=="undefined"||number==""){
								html+='<li class="mui-table-view-cell mui-media" style="padding-right:15px">';
								html+='		<div class="mui-pull-left order-menuimg">';
								html+='			<img class="mui-media-object mui-pull-left" src="http://fm-fm.img-cn-shanghai.aliyuncs.com/' +filePath+ '" style="max-width:76px">';
								html+='		</div>';
								html+='		<div class="mui-media-body" id="promptfoodLi_'+i+'">';
								html+='			<p class="food-name">'+food.NAME+'</p>';
								html+='			<div class="bx">';
								html+='				<div class="price">';
								html+='					<span>￥'+food.PRICE+'</span>/位';
								html+='				</div>';
								html+='				<div class="bx-choose-box">';
								html+='					<span class="iconfont icon-tubiao prompt-subtract"></span>';
								html+='					<em class="choose-number" onClick=checkTableware(\''+food.FOOD_ID+'\',\''+food.NAME+'\',this) id="num_'+food.FOOD_ID+'">1</em>';
								html+='					<span class="iconfont icon-tianjia tianjia prompt-add"></span>';
								html+='				</div>';
								html+='			</div>';
								html+='		</div>';
								html+='</li>';
							}else{
								html+='<li class="mui-table-view-cell mui-media" style="padding-right:15px">';
								html+='		<div class="mui-pull-left order-menuimg">';
								html+='			<img class="mui-media-object mui-pull-left" src=""http://fm-fm.img-cn-shanghai.aliyuncs.com/' +filePath+ '" style="max-width:76px">';
								html+='		</div>';
								html+='		<div class="mui-media-body" id="promptfoodLi_'+i+'">';
								html+='			<p class="food-name">'+food.NAME+'</p>';
								html+='			<div class="bx">';
								html+='				<div class="price">';
								html+='					<span>￥'+food.PRICE+'</span>/位';
								html+='				</div>';
								html+='				<div class="bx-choose-box">';
								html+='					<span class="iconfont icon-tubiao prompt-subtract"></span>';
								html+='					<em class="choose-number" onClick=checkTableware(\''+food.FOOD_ID+'\',\''+food.NAME+'\',this) id="num_'+food.FOOD_ID+'">'+number+'</em>';
								html+='					<span class="iconfont icon-tianjia tianjia prompt-add"></span>';
								html+='				</div>';
								html+='			</div>';
								html+='		</div>';
								html+='</li>';
							}
						}
						$('#tablewareUl').append(html);
					}else{
						for (var int = 0; int < promptFoods.length; int++) {
							var food = promptFoods[int];
							var promptFood = $("div[foodid='" + food.FOOD_ID + "']");
							var number =promptFood.find('.choose-number').html();
							if(typeof(number)=="undefined"||number==""){
								$('#tablewareDiv').find('#num_'+food.FOOD_ID).html(1);
							}else{
								$('#tablewareDiv').find('#num_'+food.FOOD_ID).html(number);
							}
						}
					}
					// 确认选择
					$('#tablewareDivBtn').on('click', function() {
						$('#layer-information').hide();
					    $('#tablewareDiv').hide();
							for (var int = 0; int < promptFoods.length; int++) {
								var food = promptFoods[int];
								//var prompt = $(this);
								//var amount = prompt.parent().parent();
								//var number = parseFloat(amount.find('.choose-number').html());
								var number = parseFloat($("#num_"+food.FOOD_ID).html()); 
								
								if (number > 0) {
									me.addShoppingCart({
										food:{
											foodId : food.foodId,
											quantity : number,
											name : food.NAME,
											price : food.PRICE,
										},
										menuName : food.SHOP_MENU_NAME,
										menuId : food.SHOP_MENU_ID
									});
								}
							}
							submitShoppingCar();
					});
					// 弹出选择加
					$('.prompt-add').on('click', function(e) {
						var prompt = $(this);
						var amount = prompt.prev();
						amount.html(parseFloat(amount.html()) + 1);
					});
					// 弹出选择减
					$('.prompt-subtract').on('click', function() {
						var prompt = $(this);
						var amount = prompt.next();
						if (parseFloat(amount.html()) == 1) {
							return;
						} else {
							amount.html(parseFloat(amount.html()) - 1);
						}
					});
				}
			});
			
			// wifi信息
			if (_global.WIFI_PASSWORD == '') {
				$("#wifi").remove();
			} else {
				$('#wifi-name').html(_global.WIFI_NAME);
				$('#wifi-password').html(_global.WIFI_PASSWORD);
				// 查看wifi
				$("#wifi").on('click', function() {
					$('#layer').show();
				});
				$(".mui-popup-buttons").on('click', function() {
					$('#layer').fadeOut();
				})
			}
			//会员中心
			$('#myAss').on('click', function() {
				location.href = rootpath+'/weixinHy/toAss?RAMADHIN='+_global.RAMADHIN+'&CHANNEL='+ _global.CHANNEL+'&BUSINESS_ID=' + _global.BUSINESS_ID + '&OPEN_ID=' + _global.OPENID + '&F_TYPE=' + _global.F_TYPE;
			});
			//我的订单
			$('#myOrder').on('click', function() {
				 location.href = rootpath+'/jhh/toOrderList';
			});
			
			// 显示购物车弹出信息
			me.shoppingCarShow = function() {
				$('#layer-shopping').show();
				$('.shopping-carlayer').show();
				$("#layer-shopping").on('click', function() {
					$('#layer-shopping').hide();
					$('.shopping-carlayer').hide();
				})
			}

			$('.shopping-cartbox').on('click', function() {
				$('#shoppingCar_bottom').html('');
				if (shoppingCart.length > 0) {
					var html = "";
					for (var i = 0; i < shoppingCart.length; i++) {
						var foodTagN="";
						var foodNameS=shoppingCart[i].food.name.split("_");
						if(foodNameS.length>1){
							foodTagN=foodNameS[1];
						}
						 
						html += "<dl  FOOD_ID='" + shoppingCart[i].food.foodId + "' FOOD_NAME='" + shoppingCart[i].food.name + "' FOOD_PRICE='" + shoppingCart[i].food.price + "' FOOD_QUANTITY=" + shoppingCart[i].food.quantity + " MENU_ID='" + shoppingCart[i].food.menuId + "'><dt><span class='mui-block'>" + foodNameS[0] + "</span><span class='mui-block mui-h6' style='line-height:10px;''>"+foodTagN+"</span></dt>";
						html += " <dd class=\"mui-text-center color-orange\"><span class='mui-h6 color-orange'>￥</span>" + shoppingCart[i].food.price + "</dd>";
						html += " <dd class=\"mui-text-right\">";
						html += " <div class=\"choose-box\">";
						html += "<span class=\"iconfont icon-tubiao bottomCarSubtractBtn\"></span>";
						html += "<em class=\"choose-number\">" + shoppingCart[i].food.quantity + "</em>";
						html += "<span class=\"iconfont icon-add add1 bottomCarAddBtn\"></span></div></dd></dl>";
					}
					$('#shoppingCar_bottom').append(html);
					me.shoppingCarShow();
				}

				$(".bottomCarSubtractBtn").on('click', function() {
					var FOOD_ID = $(this).parents('dl').attr('FOOD_ID');
					var FOOD_NAME = $(this).parents('dl').attr('FOOD_NAME');
					var FOOD_PRICE = $(this).parents('dl').attr('FOOD_PRICE');
					var FOOD_QUANTITY = $(this).parents('dl').attr('FOOD_QUANTITY');
					var menu_id = $(this).parents('dl').attr('menu_id');
					var QUANTITY = Number(FOOD_QUANTITY) - 1;
					if (QUANTITY >= 0) {
						$(this).parents('dl').attr('FOOD_QUANTITY', QUANTITY);
						$(this).parent().find('em').html(QUANTITY);
 
						// 同步上面的
						$('.number_' + FOOD_ID).html(QUANTITY);
						me.addShoppingCart({
							food:{
								foodId : FOOD_ID,
								quantity : -1,
								name : FOOD_NAME,
								price : FOOD_PRICE
							}
						});
						if (QUANTITY == 0) {
							$(this).parents('dl').remove();
							/*$('.number_' + FOOD_ID).next().attr('class', 'iconfont icon-21tianjia tianjia add1');*/
							$('.number_' + FOOD_ID).next().attr('class', 'iconfont icon-add tianjia add1');
							$('.number_' + FOOD_ID).prev().remove();
							$('.number_' + FOOD_ID).remove();
						}
					}
					//改变规格 
					saveSession(FOOD_ID,-1);
					// 如果购物车为空,隐藏
					if (shoppingCart.length == 0) {
						$('#layer-shopping').hide();
						$('.shopping-carlayer').hide();
						$(".bottom-confirm").hide();
						$("#end").hide();
						$("#Totalempty").show();
						$("#Total").hide();
					}
				});
				$(".bottomCarAddBtn").on('click', function() {
					var FOOD_ID = $(this).parents('dl').attr('FOOD_ID');
					var FOOD_NAME = $(this).parents('dl').attr('FOOD_NAME');
					var FOOD_PRICE = $(this).parents('dl').attr('FOOD_PRICE');
					var FOOD_QUANTITY = $(this).parents('dl').attr('FOOD_QUANTITY');
					var menu_id = $(this).parents('dl').attr('menu_id');
					$(this).parents('dl').attr('FOOD_QUANTITY', Number(FOOD_QUANTITY) + 1);
					$(this).parent().find('em').html(Number(FOOD_QUANTITY) + 1);
					 
//					if(typeof($("#tm"+FOOD_ID).html())!="undefined"){
//						var menucategory = $("a[parent='parent" + _global.BUSINESS_ID + "']");
//						me.setMenucategory( _global.BUSINESS_ID, menucategory, 1);
//					}
					
					// 同步上面的
					$('.number_' + FOOD_ID).html(Number(FOOD_QUANTITY) + 1);
					//改变规格右上角数量
					saveSession(FOOD_ID,1);
					me.addShoppingCart({
						food:{
							foodId : FOOD_ID,
							quantity : 1,
							name : FOOD_NAME,
							price : FOOD_PRICE
						}
					});
				});
			});

			// 底部购物车 添加按钮
			(function() {

				var controlsElem = document.getElementById("segmentedControls");
				var contentsElem = document.getElementById("segmentedControlContents");
				var controlListElem = controlsElem.querySelectorAll('.mui-control-item');
				var contentListElem = contentsElem.querySelectorAll('.mui-control-content2');
				var controlWrapperElem = controlsElem.parentNode;
				var controlWrapperHeight = controlWrapperElem.offsetHeight;
				var controlMaxScroll = controlWrapperElem.scrollHeight - controlWrapperHeight;// 左侧类别最大可滚动高度
				var maxScroll = contentsElem.scrollHeight - contentsElem.offsetHeight;// 右侧内容最大可滚动高度
				var controlHeight = controlListElem[0].offsetHeight;// 左侧类别每一项的高度
				var controlTops = []; // 存储control的scrollTop值
				var contentTops = [ 0 ]; // 存储content的scrollTop值
				var length = contentListElem.length;
				for (var i = 0; i < length; i++) {
					controlTops.push(controlListElem[i].offsetTop + controlHeight);
				}

				for (var i = 1; i < length; i++) {
					var offsetTop = contentListElem[i].offsetTop;
					if (offsetTop + 100 >= maxScroll) {
						var height = Math.max(offsetTop + 100 - maxScroll, 100);
						var totalHeight = 0;
						var heights = [];
						for (var j = i; j < length; j++) {
							var offsetHeight = contentListElem[j].offsetHeight;
							totalHeight += offsetHeight;
							heights.push(totalHeight);
						}
						for (var m = 0, len = heights.length; m < len; m++) {
							contentTops.push(parseInt(maxScroll - (height - heights[m] / totalHeight * height)));
						}
						break;
					} else {
						contentTops.push(parseInt(offsetTop));
					}
				}
				contentsElem.addEventListener('scroll', function() {
					var scrollTop = contentsElem.scrollTop;
					for (var i = 0; i < length; i++) {
						var offsetTop = contentTops[i];
						var offset = Math.abs(offsetTop - scrollTop);
						if (scrollTop < offsetTop) {
							if (scrollTop >= maxScroll) {
								onScroll(length - 1);
							} else {
								onScroll(i - 1);
							}
							break;
						} else if (offset < 20) {
							onScroll(i);
							break;
						} else if (scrollTop >= maxScroll) {
							onScroll(length - 1);
							break;
						}
					}
				});
				var lastIndex = 0;
				// 监听content滚动
				var onScroll = function(index) {
					if (lastIndex !== index) {
						lastIndex = index;
						var lastActiveElem = controlsElem.querySelector('.mui-active');
						lastActiveElem && (lastActiveElem.classList.remove('mui-active'));
						var currentElem = controlsElem.querySelector('.mui-control-item:nth-child(' + (index + 1) + ')');
						currentElem.classList.add('mui-active');
						// 简单处理左侧分类滚动，要么滚动到底，要么滚动到顶
						var controlScrollTop = controlWrapperElem.scrollTop;
						if (controlScrollTop + controlWrapperHeight < controlTops[index]) {
							controlWrapperElem.scrollTop = controlMaxScroll;
						} else if (controlScrollTop > controlTops[index] - controlHeight) {
							controlWrapperElem.scrollTop = 0;
						}
					}
				};
				// 滚动到指定content
				var scrollTo = function(index) {
					contentsElem.scrollTop = contentTops[index];
				};
				mui(controlsElem).on('tap', '.mui-control-item', function(e) {
					scrollTo(this.getAttribute('data-index'));
					return;
				});
			})();
		}
	},
	localData : function(table, settings) {
		table = table || 'shoppingCar';

		if (!window.JSON || !window.JSON.parse)
			return;

		// 如果settings为null，则删除表
		if (settings === null) {
			return delete localStorage[table];
		}

		settings = typeof settings === 'object' ? settings : {
			key : settings
		};

		try {
			var data = JSON.parse(localStorage[table]);
		} catch (e) {
			var data = {};
		}
		if (settings.value)
			data[settings.key] = settings.value;
		if (settings.remove)
			delete data[settings.key];
		localStorage[table] = JSON.stringify(data);
		return settings.key ? data[settings.key] : data;
	}
};

//规格弹框
function mutliType(foodId,foodName,foodPrice){
	$("#mutliName").html(foodName);
	amountBtnDefault();
	$("#partIdi").html('0');
	$("#motliText").html('');	 
	var obj=$("#tagQ"+foodId);
	var tagam=obj.find(".mui-badge-orange");
	if(tagam.length==1){
		$("#partIdi").html(tagam.html());
	}
	var tagAmount=0;
	var html="";
	var mutli=sessionStorage.getItem("mutli"+foodId);
	var foodtagId;
	if(mutli==null){
		$.post(rootpath + '/jhh/getMutliType.do', {
			foodId : foodId
		}, function(result) {
			if(result.foodTags.length==0){
				mui.toast('暂无可选规格', {
					duration : 'long',
					type : 'div'
				});
				return;
			}
			mutli=JSON.stringify(result.foodTags);
			sessionStorage.setItem("mutli"+foodId,mutli);
			$("#motliText").html('<dt class="popover-choosetit">规格</dt>');
			$("#layerType").show();
			for(var i=0;i<result.foodTags.length;i++){
				var foodTag=result.foodTags[i];
				foodtagId=foodTag.foodId+'_'+foodTag.foodTagId;
				var param=foodTag.price+',"'+foodtagId+'","'+(foodName+'_'+foodTag.name)+'"';
				if(i==0){
					initAmountBtn(foodtagId,param);							
					$("#multiPrice").html('<em>￥</em>'+foodTag.price);
					html+='<dd class="taste-item active" onclick=changeText('+param+',this)>'+foodTag.name+'</dd>';
					
					$("#tagPrice").val(foodTag.price);
					$("#tagId").val(foodTag.foodId+"_"+foodTag.foodTagId);
					$("#tagName").val(foodName+'_'+foodTag.name);
				}else{
					html+='<dd class="taste-item" onclick=changeText('+param+',this)>'+foodTag.name+'</dd>';
				}
			}
			$("#motliText").append(html);
		}, 'json').error(function() {
			mui.toast('暂无可选规格', {
				duration : 'long',
				type : 'div'
			});
		});
	}else{
		var result=JSON.parse(mutli);
		if(result.length==0){
			mui.toast('暂无可选规格', {
				duration : 'long',
				type : 'div'
			});
			return;
		}
		$("#motliText").html('<dt class="popover-choosetit">规格</dt>');
		$("#layerType").show();
		for(var i=0;i<result.length;i++){
			foodtagId=result[i].foodId+'_'+result[i].foodTagId;
			var param=result[i].price+',"'+foodtagId+'","'+(foodName+'_'+result[i].name)+'"';
			if(i==0){
				initAmountBtn(foodtagId,param);
				$("#multiPrice").html('<em>￥</em>'+result[i].price);
				html+='<dd class="taste-item active" onclick=changeText('+param+',this)>'+result[i].name+'</dd>';
				
				$("#tagPrice").val(result[i].price);
				$("#tagId").val(result[i].foodId+"_"+result[i].foodTagId);
				$("#tagName").val(foodName+'_'+result[i].name);
			}else{
				html+='<dd class="taste-item" onclick=changeText('+param+',this)>'+result[i].name+'</dd>';
			}
		
		}
		$("#motliText").append(html);
	}
	
}
//改变样式和价格
function changeText(price,foodtagId,foodName,obj){
	var param=price+',"'+foodtagId+'","'+foodName+'"';
	initAmountBtn(foodtagId,param);
	 $(".taste-item").removeClass("active");
	$(obj).addClass("active");
	$("#multiPrice").html('<em>￥</em>'+price);
	$("#tagId").val(foodtagId);
	$("#tagName").val(foodName);
	$("#tagPrice").val(price);
	
	$('#numlayer-input-foodid').val(foodtagId);
	$('#numlayer-name').html(foodName);
	$('#numlayer-input-price').val(price);
}

//改变规格右上角数量 
function changeTagQ(foodtagId,amount){
	var obj=$("#tagQ"+foodtagId.split("_")[0]);
	var tagam=obj.find(".mui-badge-orange");
	if(amount>=1){
		//加
		if(tagam.length==0){
			$("#partIdi").html(1);
			obj.append('<span class="mui-badge mui-badge-orange">'+1+'</span>');
		}else{
			var quantity=parseFloat(tagam.html())+amount;
			tagam.html(quantity);
			$("#partIdi").html(quantity);
		}
	}else if(amount<0){
		//减
		var val=parseFloat(tagam.html())+amount;
		if(val==0){
			$("#partIdi").html("0");
			tagam.remove();
		}else if(val>0){
			tagam.html(val);
			$("#partIdi").html(val);
		}
	}else if(amount==0){
		tagam.remove();
	}
}
//初始化规格的 加减按钮
function  initAmountBtn(foodtagId,param){
	var initAmount=sessionStorage.getItem("food"+foodtagId);
	if(initAmount==null|| parseFloat(initAmount)==0){
		amountBtnDefault();
		initAmount=0;
	}
	if(initAmount>0){
		if($("#chooseBox").find(".icon-tubiao").length==0){
			$("#amountBox").before('<span class="iconfont icon-tubiao"></span><em onClick=numCheck('+param+') id="num_'+foodtagId+'" class="choose-number number_' + foodtagId + '">'+initAmount+'</em>');
			$("#amountBox").removeClass("icon-add");
			$("#amountBox").addClass("icon-tianjia");
		}else{
			$("#amountBox").prev().prev().remove();
			$("#amountBox").prev().remove();
			$("#amountBox").before('<span class="iconfont icon-tubiao"></span><em onClick=numCheck('+param+') id="num_'+foodtagId+'" class="choose-number number_' + foodtagId + '">'+initAmount+'</em>');
		}
		$("#cartBox").hide();
		$("#chooseBox").show();
		me.bindReduceEvent();
	}
}

function amountBtnDefault(){
	$("#multiPrice").html('<em>￥</em>0');
	$("#tagId").val('');
	if($("#chooseBox").find(".icon-tubiao").length>0){
		$("#amountBox").attr('class', 'iconfont icon-add tianjia add1');
		$("#amountBox").prev().prev().remove();
		$("#amountBox").prev().remove();
		$("#cartBox").show();
		$("#chooseBox").hide();
	}
}
	//保存到session中
	function saveSession(foodtagId,val){
		var initAmount=parseFloat(sessionStorage.getItem("food"+foodtagId));
		if(initAmount==null || parseFloat(initAmount)==0){
			initAmount=0;
		}else{
			initAmount=parseFloat(initAmount);
		}
		if(initAmount+val>=0){
			sessionStorage.setItem("food"+foodtagId,initAmount+val);
			changeTagQ(foodtagId,val);
		}
	}
	function tagCart(){
		if($("#tagId").val()==""){
			return;
		}
		$("#cartBox").hide();
		$("#chooseBox").show();
		$("#amountBox").click();
		 
	}
//消费必选显示数量选择弹框
	function checkTableware(foodid,foodName,obj){
		var num = $("#num_"+foodid).html();  
		$('#numlayer-name').html(foodName);
		$('#numlayer-input-afterNum').val(num);
		$('#numlayer-input-foodid').val(foodid);
		$('#numlayer-input-shopMenu').val("tableware");
		$("#numlayer").show();
	}
//规格菜品显示数量选择弹框
	function numCheck(price,foodtagid,foodName){
		$('#numlayer-input-foodid').val(foodtagid);
		var num = $("#num_"+foodtagid).html();  
		$('#numlayer-name').html(foodName);
		$('#numlayer-input-afterNum').val(num);
		$('#numlayer-input-beforeNum').val(num);
		$('#numlayer-input-price').val(price);
		$('#numlayer-input-foodid').val(foodtagid);
		$('#numlayer-input-shopMenu').val("tag");
		$("#numlayer").show();
	}
//菜品显示数量选择弹框
	function checkNum(foodid,name,prices,obj){
		return ;//暂时不做
		
		if(foodid.length > 60){
			$('#numlayer-name').html(name);
			var num = $("#num_"+foodid).html();  
			$('#numlayer-input-afterNum').val(num);
			$('#numlayer-input-beforeNum').val(num);
			$('#numlayer-input-foodid').val(foodid);
			$('#numlayer-input-price').val(prices);
			$('#numlayer-input-shopMenu').val("tag");
			$("#numlayer").show();
		}else{
			var parent=$($(obj).parent().parent());
			var parentId= parent.attr('parentId');
			var price = parent.attr('price');
			var num = $("#num_"+foodid).html();  
			$("#numlayer").show();
			$('#numlayer-name').html(name);
			$('#numlayer-input-foodid').val(foodid);
			$('#numlayer-input-parentId').val(parentId);
			$('#numlayer-input-afterNum').val(num);
			$('#numlayer-input-beforeNum').val(num);
			$('#numlayer-input-price').val(price);
			$('#numlayer-input-shopMenu').val("ptnum");
		}
	}
//隐藏数量选择弹框(取消)
	$('#hideNum').on('click',function(){
		$("#numlayer").hide();
	})
	
//确定(数量选择弹框)
	$('#saveNum').on('click',function(){
		var id = $('#numlayer-input-foodid').val();
		var shopmenu = $('#numlayer-input-shopMenu').val();
		if(shopmenu == "tableware"){
			var afterNum = $('#numlayer-input-afterNum').val();
			$("#numlayer").hide();
			$("#num_"+id).html(afterNum);
		}else{
			if(id.length > 60){
				var strs= new Array(); //定义一数组 
				strs=id.split("_"); //字符分割 
				var foodtagid=strs[0];
				var tmparentId=$("#tm"+foodtagid).attr('parentId');
				var ftdparentId=$("#ftd"+foodtagid).attr('parentId');
			}else{
				var tmparentId=$("#tm"+id).attr('parentId');
				var ftdparentId=$("#ftd"+id).attr('parentId');
			}
			var afterNum = $('#numlayer-input-afterNum').val();
			var beforeNum = $('#numlayer-input-beforeNum').val();
			var price = $('#numlayer-input-price').val();
			var foodName = $('#numlayer-name').val();
			
			if(afterNum == ''){
				 mui.toast('请填写数量', {
						duration : 'long',
						type : 'div'
					});
			}else{
				$("#numlayer").hide();
				$(".number_"+id).html(afterNum);
				var num = parseInt(afterNum)-parseInt(beforeNum);
				var total= parseFloat(accMul(num,price)).toFixed(2);
				var menucategory = $("a[parent='parent" + ftdparentId + "']");
				//特卖
				if(tmparentId =='undefined'){
					//更改菜单的数量
					me.setMenucategory(ftdparentId, menucategory, num); 
					saveSession(id,num);
				}else{
					//更改特价菜品的数量
					var ftdmenucategory = $("a[parent='parent" + ftdparentId + "']");
					me.setMenucategory(ftdparentId, ftdmenucategory, num); 
					var tmmenucategory = $("a[parent='parent" + tmparentId + "']");
					me.setMenucategory(tmparentId, tmmenucategory, num); 
					saveSession(id,num);
				}
				
				//更改购物车内的数量
				me.addShoppingCart({
					food:{foodId:id,
						quantity : num,
						name : foodName,
						price : total
					}
					
				});
			}		
		}		
});

// 数量选择弹框修改购物车上面的小图标
function setcarCount(num) {
	var carCount = $(".shopping-carnum");
		carCount.html(parseInt(carCount.html()) + num);
		if (parseInt(carCount.html()) == 0) {
			return;
		}
};
// 数量选择弹框修改购物车总价
function setcarCountPrice(price) {
	var carCountPrice = $('#carTotalPrice');
	var currentTotalPrice = parseFloat(carCountPrice.text() == '' ? 0 : carCountPrice.text());
	if (currentTotalPrice >= 0) {
		var totalPrice = parseFloat((currentTotalPrice + parseFloat(price)).toFixed(2));
		if(totalPrice>=0){
			carCountPrice.text(totalPrice);
		}
	}
};



//乘法函数，用来得到精确的乘法结果   
//说明：javascript的乘法结果会有误差，在两个浮点数相乘的时候会比较明显。这个函数返回较为精确的乘法结果。   
//调用：accMul(arg1,arg2)   
//返回值：arg1乘以arg2的精确结果   
function accMul(arg1,arg2){   
 var m=0,s1=arg1.toString(),s2=arg2.toString();   
 try{m+=s1.split(".")[1].length}catch(e){}   
 try{m+=s2.split(".")[1].length}catch(e){}   
 return Number(s1.replace(".",""))*Number(s2.replace(".",""))/Math.pow(10,m)   
31.} 







